var datagrid;
var dialog;
var dialog_flow;
var dialog_operator;
var flowDataGrid;
var operatorDataGrid;
var dialog_operator_form;
var dialog_flow_form;
var dialog_apply_form;
var assetsDataGrid;
var dialog_assets;
$(function () {
    $("#validation_flag").textbox({
        onChange: function (newValue, oldValue) {
            //设成默认颜色
            if (newValue == "未承认") {
                $('#validation_flag').textbox('textbox').css('background', '#cc2b3c')
            } else if (newValue == "已承认") {
                $('#validation_flag').textbox('textbox').css('background', '#23cc3d')
            } else {
                $('#validation_flag').textbox('textbox').css('background', '')
            }
        }
    });
    $("#generic_flag").combobox({
        onChange: function (newValue, oldValue) {
            console.log("事件被触发");
            //设成默认颜色
            if (newValue == "1") {
                $('#erpInfo').combobox({
                    readonly: true,
                    required: false
                });
                $("#use_to").textbox({
                    readonly: false,
                    required: true
                });
                setVal("#erpInfo", "");
                setVal("#conProductNum", "");
                setVal("#conDept", "");
                setVal("#sdOrderAmount", "");
                setVal("#cusSentToName", "");
            } else if (newValue == "0") {
                $('#erpInfo').combobox({
                    readonly: false,
                    required: true
                });
                $("#use_to").textbox({
                    readonly: true,
                    required: false
                });
            }
        }
    });

    //物资申请列表
    datagrid = $('#dg').datagrid({
        method: "get",
        url: getRootPath() + '/mtManage/assets/list_applyGrid',
        fit: true,
        border: true,
        idField: 'id',
        striped: true,
        pagination: true,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        checkOnSelect: true,
        toolbar: '#tb',
        fitColumns: true,
        // sortName: "state",
        // sortOrder: "asc",
        remoteSort: false,
        columns: [[
            {field: 'ck', checkbox: true},
            {
                field: 'erp_no', title: 'ERP单号', width: 100,
                formatter: function (value, row, index) {
                    if (row.erp_no == "") {
                        value = "通用";
                        return value;
                    } else {
                        return value;
                    }

                }
            },
            {
                field: 'conProductNum', title: '机型', width: 100,
                formatter: function (value, row, index) {
                    if (row.erp_no == "") {
                        value = "";
                        return value;
                    } else {
                        return value;
                    }

                }
            },
            {
                field: 'conDept', title: '事业部', width: 100,
                formatter: function (value, row, index) {
                    if (row.erp_no == "") {
                        value = "";
                        return value;
                    } else {
                        return value;
                    }

                }
            },
            {field: 'apply_dept', title: '申购部门', hidden: true},
            {
                field: 'sdOrderAmount', title: '订单数量', width: 100,
                formatter: function (value, row, index) {
                    if (row.erp_no == "") {
                        value = "";
                        return value;
                    } else {
                        return value;
                    }

                }
            },
            {
                field: 'cusSentToName', title: '发往地', width: 100,
                formatter: function (value, row, index) {
                    if (row.erp_no == "") {
                        value = "";
                        return value;
                    } else {
                        return value;
                    }

                }
            },
            {field: 'node_seq', title: 'node_seq', hidden: true},
            {field: 'turn_node_seq', title: 'turn_node_seq', hidden: true},
            {field: 'currentuser', title: '当前责任人', width: 100},
            {field: 'apply_man', title: '申请人', width: 100, sortable: true},
            {field: 'apply_time', title: '申请时间', width: 100, sortable: true},
            {field: 'print_count', title: '打印次数', width: 100},
            {field: 'print_time', title: '打印时间', width: 100},
            {field: 'state', title: '状态', width: 100, sortable: true},
            {
                field: 'urgent_flag', title: '加急', width: 100, sortable: true,
                formatter: function (value, row, index) {
                    if (value == null) {
                        value = "";
                    } else {
                        if (value == "1") {
                            // return 'background-color:#FFF68F;';
                            return "<span style='color:red' title='加急'>加急</span>";
                        } else {
                            return "<span title='不加急'>不加急</span>";
                        }
                        // if (value == "1") {
                        //     // return 'background-color:#FFF68F;';
                        //     return "<span style='color:red' title='" + value + "'>" + value + "</span>";
                        // } else {
                        //     return "<span title='" + value + "'>" + value + "</span>";
                        // }
                    }
                }
            },
            {
                field: 'remark', title: '备注', width: 100,
                formatter: function (value, row, index) {
                    if (value == null) {
                        value = "";
                    } else {
                        return "<span title='" + value + "'>" + value + "</span>";
                    }
                }
            },
            {field: 'for_workshop', title: '使用车间', hidden: true}
        ]],
        onLoadSuccess: function (data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});

            if (data) {
                $.each(data.rows,
                    function (index, item) {
                        if (item.checked) {
                            $('#dg').datagrid('checkRow', index);
                        }
                    });
            }
        },
        onSelect: function (index, row) {

        },
        onDblClickRow: function (index, row) {
            operator_apply(row);
        },
        queryParams: {
            // username: username
            // username: $('#username').val(),
            // mobile: $('#mobile').val(),
            // gender: $('#gender').val()
        }
    });

    //新增物料dialog
    $.getJSON(getRootPath() + "/mtManage/assets/list_erp", function (json) {
        $('#erpInfo').combobox({
            data: json.data, //获取到的json 数据
            valueField: "conErpNo",
            textField: "conErpNo",
            onSelect: function (record) {
                if (record.conErpNo != "") {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/getErpInfo",
                        loading: true,
                        data: {
                            conErpNo: record.conErpNo
                        },
                        success: function (data) {
                            if (data.code == 200) {
                                var erpInfo = data.data[0];
                                setVal("#conProductNum", erpInfo.conProductNum);
                                setVal("#conDept", erpInfo.conDept);
                                setVal("#sdOrderAmount", erpInfo.sdOrderAmount);
                                setVal("#cusSentToName", erpInfo.cusSentToName);
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        });

    });


    $("#apply_time_span").hide();
    $("#apply_time_td").hide();
    $("#backto_user_td").hide();
    $("#backto_user_span").hide();
    $("#backto_time_span").hide();
    $("#backto_time_td").hide();
    $("#besureout_user_span").hide();
    $("#besureout_user_td").hide();
    $("#besureout_time_span").hide();
    $("#besureout_time_td").hide();
    $("#refusereason_user_span").hide();
    $("#refusereason_user_td").hide();
    $("#refusereason_span").hide();
    $("#refusereason_td").hide();
    $("#refusereason_time_span").hide();
    $("#refusereason_time_td").hide();
    $("#out_stockin_type_text").hide();
    $("#out_stockin_type").hide();
    $("#return_plandate_text").hide();
    $("#return_plandate_td").hide();
    $("#currentuser_span").hide();
    $("#currentuser_td").hide();


    if (username != "钱意") {
        $("#btn-config").hide();
        $("#btn-export").hide();
    }

    $("#erp_no_s").textbox('textbox').keydown(function (e) {
        if (e.keyCode == 13) {
            queryApply();
        }
    });
    $("#conProductNum_s").textbox('textbox').keydown(function (e) {
        if (e.keyCode == 13) {
            queryApply();
        }
    });
    $("#department_s").textbox('textbox').keydown(function (e) {
        if (e.keyCode == 13) {
            queryApply();
        }
    });
    $("#cusSentToName_s").textbox('textbox').keydown(function (e) {
        if (e.keyCode == 13) {
            queryApply();
        }
    });
    $("#apply_name_s").textbox('textbox').keydown(function (e) {
        if (e.keyCode == 13) {
            queryApply();
        }
    });
    $("#currentuser_s").textbox('textbox').keydown(function (e) {
        if (e.keyCode == 13) {
            queryApply();
        }
    });


});

//查询操作员
function queryOperator() {
    var row_flow = flowDataGrid.datagrid('getSelected');
    if (row_flow == null) {
        return;
    }
    var id = row_flow.id;
    $(operatorDataGrid).datagrid('load', {
            id: id
        }
    );
}

//流程查询
function queryFlow() {
    $(flowDataGrid).datagrid('load');
}


//物资查询
function queryAssets() {
    var typeNo = $("#typeNo_dlg").val().trim();
    var typeName = $("#assets_name").val().trim();
    var typeSizes = $("#sizes_name").val().trim();
    $('#assets-dg').datagrid('reload', {
        type_dtl_no: typeNo,
        type_dtl_name: typeName,
        sizes: typeSizes
    });

}


//物资查询apply
function queryAssets_y() {
    var row_apply = datagrid.datagrid('getSelected');
    console.log(row_apply);
    if (row_apply == null) {
        U.msg('请先选中');
        return;
    }
    var assets_name_y = $("#assets_name_y").val().trim();
    var sizes_y = $("#sizes_y").val().trim();

    $('#assets_dg_apply').datagrid({
        method: "get",
        url: getRootPath() + '/mtManage/assets/list_applyByScreen',
        fit: true,
        border: true,
        // idField: 'apply_id',
        striped: true,
        pagination: true,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        checkOnSelect: true,
        fitColumns: true,
        toolbar: '#tb_apply',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'apply_id', title: 'apply_id', hidden: true},
            {field: 'erp_no', title: 'ERP', width: 100},
            // {field: 'apply_dept', title: '部门', width: 100},
            {field: 'apply_man', title: '申请人', width: 100},
            // {field: 'return_plandate', title: '预计归还',  width: 100},
            {field: 'apply_time', title: '申请时间', width: 100},
            {field: 'state', title: '状态', width: 100},
            {field: 'currentuser', title: '当前责任人', width: 100},
            // {field: 'dept_name', title: '物资部门', width: 100},
            // {field: 'type_main_name', title: '物资类别', width: 100},
            {field: 'type_dtl_name', title: '物资名称', width: 100},
            // {field: 'type_dtl_no', title: '物资编号', width: 100},
            {field: 'model', title: '型号', width: 100},
            {field: 'sizes', title: '规格', width: 100},
            // {field: 'out_type', title: '出库类型', width: 100},
            {field: 'apply_num', title: '申请数量', width: 100},
            // {field: 'backto_user', title: '打回人', width: 100},
            // {field: 'backto_time', title: '打回时间', width: 100},
            // {field: 'besureout_user', title: '发放人',  width: 100},
            // {field: 'besureout_time', title: '发放时间',  width: 100},
            // {field: 'refusereason_user', title: '拒绝发放人',  width: 100},
            // {field: 'refusereason', title: '拒绝发放原因',  width: 100},
            // {field: 'operator_record', title: '操作记录', width: 100},
            {field: 'apply_reason', title: '申请原因', width: 100},
            {field: 'stock_num', title: '库存数量', width: 100},
            {field: 'demand_time', title: '需求日期', width: 100},
            {
                field: 'remark', title: '备注', width: 100,
                formatter: function (value, row, index) {
                    if (value == null) {
                        value = "";
                    } else {
                        return "<span title='" + value + "'>" + value + "</span>";
                    }
                }
            },
            {field: 'for_workshop', title: '使用车间', hidden: true}
            // {
            //     field: 'operate', title: '操作', width: 100,
            //     formatter: function (value, row, index) {
            //         var d = "<a onclick='remove_flow(" + row.id + ")' class='button-delete button-red'>删除</a>";
            //         var e = "<a onclick='edit_flow(" + row.id + ")' class='button-edit button-blue'>编辑</a>";
            //         var r = "<a onclick='getOperator(" + row.id + ")' class='button-edit button-teal'>操作员配置</a>";
            //         if (row.serial_flag == '2') {//物资申请
            //             return e + '  ' + d;
            //         } else {
            //             return e + '  ' + d + '  ' + r;
            //         }
            //     }
            //
            // }
        ]],
        // onLoadSuccess: function (data) {
        //     $('.button-delete').linkbutton({});
        //     $('.button-edit').linkbutton({});
        // },
        queryParams: {
            "type_dtl_name": assets_name_y,
            "sizes": sizes_y,
            "apply_order_info_id": row_apply.id
        },
        onDblClickRow: function (index, row) {
            // setVal("#type_main_name", row.type_main_name);
            // $("#apply_id").val(row.apply_id);
            //修改物资申请
            operator_apply_info();
        }
    });

}

// 导出物资申请excel
function exportApplyData() {
    var row_apply = datagrid.datagrid('getSelected');
    console.log(row_apply);
    if (row_apply == null) {
        U.msg('请先选中');
        return;
    }
    var erp_no = row_apply.erp_no;
    // var apply_man = $("#apply_man_s").val();
    // window.location.href = getRootPath() + '/mtManage/assets/exportApplyData?erp_no=' + erp_no
    //     + '&apply_man=' + apply_man;
    window.location.href = getRootPath() + '/mtManage/assets/exportApplyData?erp_no=' + erp_no;
}

//添加操作员
function add_operator() {

    var row_flow = flowDataGrid.datagrid('getSelected');
    if (row_flow == null) {
        return;
    }
    var node_seq = row_flow.node_seq;
    var node_name = row_flow.node_name;
    $.getJSON(getRootPath() + "/mtManage/assets/list_dept", function (json) {
        $('#deptInfo').combobox({
            data: json.data, //获取到的json 数据
            valueField: "dept_id",
            textField: "dept_name"
        });
    });

    $.getJSON(getRootPath() + "/mtManage/assets/list_user", function (json) {
        $('#userInfo').combobox({
            data: json.data, //获取到的json 数据
            valueField: "user_id",
            textField: "user_name"
        });
    });
    dialog_operator_form = $("#dlg_operator_form").dialog({
        title: '添加操作员信息',
        width: 340,
        height: 300,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_operator").form('validate');
                var dept_id = $("#deptInfo").combobox("getValue");
                var dept_name = $("#deptInfo").combobox("getText");
                var user_id = $("#userInfo").combobox("getValue");
                var user_name = $("#userInfo").combobox("getText");
                console.log(dept_id + "&&" + dept_name);
                var addObj = [{"name": "dept_id", "value": dept_id},
                    {"name": "dept_name", "value": dept_name},
                    {"name": "user_id", "value": user_id},
                    {"name": "user_name", "value": user_name}
                ];
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/add_operator",
                        loading: true,
                        data: getFormJson("#form_operator", "deptInfo", addObj),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialog_operator_form.dialog('close');
                                queryOperator();
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('用户已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialog_operator_form.dialog('close');
            }
        }]
    });
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    setVal("#operator_info", node_name);
    $("#operator_node").val(node_seq);

}

//流程添加
function add_flow() {
    setVal("#model_name", "");
    setVal("#node_name", "");
    setVal("#node_seq", "");
    setVal("#turn_node_seq", "");
    $("#serial_flag").combobox('setValue', "");
    dialog_flow_form = $("#dlg_flow_form").dialog({
        title: '添加流程信息',
        width: 340,
        height: 300,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_flow").form('validate');
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/add_flow",
                        loading: true,
                        data: $('#form_flow').serialize(),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialog_flow_form.dialog('close');
                                queryFlow();
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('用户已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialog_flow_form.dialog('close');
            }
        }]
    });

}

//物资添加
function add_applyByErpno() {
    var row_apply = $('#assets_dg_apply').datagrid('getSelected');
    var applys = $('#assets_dg_apply').datagrid('getRows');
    if (applys.length > 7) {
        U.msg("最多只能添加8条，请新建申请单");
        return;
    }

    dialog_apply_form = $("#dlg_apply_form").dialog({
        title: '物资申请',
        width: 680,
        height: 500,
        maximizable: false,
        modal: true,
        closed: true,
        buttons: [{
            id: "confirm_apply",
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_apply").form('validate');
                //getFormJson("#form_apply","deptInfo","");
                //console.log($('#form_apply'));
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/add_apply",
                        loading: true,
                        data: $('#form_apply').serialize() + "&id=" + row_apply.apply_order_info_id,
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialog_apply_form.dialog('close');
                                $('#assets_dg_apply').datagrid('load');
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('用户已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialog_apply_form.dialog('close');
            }
        }]
    });

    // 将订单信息改为不可编辑
    $('#erpInfo').combobox({
        readonly: true
    });

    $("#form_apply").form("clear");
    $("#confirm_apply").show();
    $("#erpInfo").combobox('setValue', row_apply.erp_no);
    console.log("--------------" + row_apply.urgent_flag);

    $("#urgent_flag_id").combobox({
        readonly: true
    });
    $("#generic_flag").combobox({
        readonly: true
    });
    setVal("#state", '待提交');
    setVal("#apply_man", row_apply.apply_man);
    $("#generic_flag").combobox('setValue', row_apply.generic_flag);
    $.ajax({
        type: 'post',
        url: getRootPath() + "/mtManage/assets/getUrgentFlag",
        data: {
            apply_order_info_id: row_apply.apply_order_info_id
        },
        success: function (data) {
            if (data.code == 200) {
                if (data.data == '1') {
                    $("#urgent_flag_id").combobox('setValue', "1");
                } else {
                    $("#urgent_flag_id").combobox('setValue', "0");
                }
            }
        },
        complete: function () {

        }
    });
    dialog_apply_form.dialog("open");
}

//物资申请查询
function queryApply() {
    $(datagrid).datagrid('load', {
            erp_no: $("#erp_no_s").val().trim(),
            conProductNum: $("#conProductNum_s").val().trim(),
            conDept: $("#department_s").val().trim(),
            cusSentToName: $("#cusSentToName_s").val().trim(),
            apply_man: $("#apply_name_s").val().trim(),
            currentuser: $("#currentuser_s").val().trim(),
            state: $("#state_s").val().trim()
        }
    );
}

//添加申请
function add_apply() {
    dialog_apply_form = $("#dlg_apply_form").dialog({
        title: '物资申请明细',
        width: 680,
        height: 500,
        maximizable: false,
        modal: true,
        closed: true,
        buttons: [{
            id: "confirm_apply",
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_apply").form('validate');
                //getFormJson("#form_apply","deptInfo","");
                //console.log($('#form_apply'));
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/add_apply",
                        loading: true,
                        data: $('#form_apply').serialize(),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialog_apply_form.dialog('close');
                                $(datagrid).datagrid('load');
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('用户已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialog_apply_form.dialog('close');
            }
        }]
    });

    // $('#erpInfo').combobox({
    //     readonly: false
    // });
    $("#urgent_flag_id").combobox({
        readonly: false
    });
    $("#generic_flag").combobox({
        readonly: false
    });

    $("#confirm_apply").show();
    $("#form_apply").form("clear");
    $("#urgent_flag_id").combobox('setValue', "0");
    $("#generic_flag").combobox('setValue', "0");
    $("#applyType_flag_id").combobox('setValue', "1");
    setVal("#state", '待提交');
    setVal("#apply_man", username);
    dialog_apply_form.dialog("open");
}

/*流程编辑*/
function edit_flow() {

    var row_flow = flowDataGrid.datagrid('getSelected');
    console.log("row_flow:" + row_flow)
    if (row_flow == null) {
        U.msg('请先选中');
        return;
    }

    var id = row_flow.id;
    var node_name = row_flow.node_name;

    $("input[name='id']").val(id);
    setVal("#model_name", row_flow.model_name);
    setVal("#node_name", row_flow.node_name);
    setVal("#node_seq", row_flow.node_seq);
    setVal("#turn_node_seq", row_flow.turn_node_seq);
    $("#serial_flag").combobox('setValue', row_flow.serial_flag);

    dialog_flow_form = $("#dlg_flow_form").dialog({
        title: '修改流程信息',
        width: 340,
        height: 300,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_flow").form('validate');
                var addObj = [];
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/update_flow",
                        loading: true,
                        data: $("#form_flow").serialize(),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                dialog_flow_form.dialog('close');
                                queryFlow();
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialog_flow_form.dialog('close');
            }
        }]
    });

    console.log(node_name);
    console.log(id);
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    setVal("#operator_info", node_name);
    $("#operator_node").val(id);
}

//物资申请编辑
function edit_apply() {
    var row_apply = $("#assets_dg_apply").datagrid('getSelected');
    if (row_apply == null) {
        U.msg('请先选中');
        return;
    }
    var id = row_apply.apply_id;
    var state = row_apply.state;
    var apply_man = row_apply.apply_man;
    var currentuser = row_apply.currentuser;

    // var cheack_falg = edit_apply_cheack(currentuser, state, apply_man);


    $("#dlg_apply_form").dialog({
        title: '编辑申请',
        width: 680,
        height: 500,
        maximizable: false,
        modal: true,
        closed: true,
        buttons: [{
            id: 'confirm_apply',
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_apply").form('validate');
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/update_apply",
                        loading: true,
                        data: $('#form_apply').serialize(),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                $("#dlg_apply_form").dialog('close');
                                $('#assets_dg_apply').datagrid("load");
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('用户已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                $("#dlg_apply_form").dialog('close');
            }
        }]
    });
    setVal("#state", row_apply.state);
    setVal("#dept_name", row_apply.dept_name);
    setVal("#type_main_name", row_apply.type_main_name);
    setVal("#type_dtl_name", row_apply.type_dtl_name);
    setVal("#type_dtl_no", row_apply.type_dtl_no);
    setVal("#model", row_apply.model);
    setVal("#sizes", row_apply.sizes);
    setVal("#return_plandate", new Date(row_apply.return_plandate).format('yyyy-MM-dd'));
    setVal("#apply_num", row_apply.apply_num);
    setVal("#apply_man", apply_man);
    setVal("#apply_dept", row_apply.apply_dept);
    setVal("#apply_time", new Date(row_apply.apply_time).format('yyyy-MM-dd'));
    setVal("#currentuser", currentuser);
    setVal("#backto_user", row_apply.backto_user);
    setVal("#backto_time", new Date(row_apply.backto_time).format('yyyy-MM-dd'));
    setVal("#besureout_user", row_apply.besureout_user);
    setVal("#besureout_time", row_apply.besureout_time);
    setVal("#refusereason", row_apply.refusereason);
    setVal("#operator_record", row_apply.operator_record);
    setVal("#demand_time", new Date(row_apply.demand_time).format('yyyy-MM-dd'));
    setVal("#stock_num", row_apply.stock_num);
    setVal("#apply_reason", row_apply.apply_reason);
    setVal("#remark", row_apply.remark);
    setVal("#dept_use", row_apply.dept_use);
    setVal("#use_to", row_apply.use_to);
    setVal("#for_workshop", row_apply.for_workshop);
    if (row_apply.validation_flag == '1') {
        setVal("#validation_flag", "已承认");
    } else if (row_apply.validation_flag == '0') {
        setVal("#validation_flag", "未承认");
    } else {
        setVal("#validation_flag", "");
    }
    $("#assets_id").val(row_apply.assets_id);
    $("#apply_id").val(id);
    $("#out_type").combobox('setValue', row_apply.out_type);

    $('#erpInfo').combobox({
        readonly: true
    });
    $("#urgent_flag_id").combobox({
        readonly: true
    });
    $("#generic_flag").combobox({
        readonly: true
    });

    $("#erpInfo").combobox('setValue', row_apply.erp_no);
    $("#generic_flag").combobox('setValue', row_apply.generic_flag);
    if (row_apply.urgent_flag == '1') {
        $("#urgent_flag_id").combobox('setValue', "1");
    } else {
        $("#urgent_flag_id").combobox('setValue', "0");
    }
    $("#dlg_apply_form").dialog('open');
    // $('#applyBtn').hide();
    // $('#checkBtn').hide();

    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    //setVal("#operator_info",node_name);
    //$("#operator_node").val(id);
}

//编辑前进行判定是否能进行编辑
function edit_apply_cheack(currentuser, state, apply_man) {
    var cheack_flag;
    U.post({
        url: getRootPath() + "/mtManage/assets/cheack_before",
        loading: true,
        async: false,
        data: {
            currentuser: currentuser,
            state: state,
            username: username,
            apply_man: apply_man
        },
        success: function (data) {
            if (data.data == true) {
                cheack_flag = 1;
            } else if (data.data == false) {
                cheack_flag = 0;
            } else {
                cheack_flag = 999;
            }
        }
    });
    return cheack_flag;
}


function operator_apply(row_apply) {
    // var row_apply = $('#dg').datagrid('getSelected');
    console.log(row_apply);
    if (row_apply == null) {
        U.msg('请先选中');
        return;
    }

    var id = row_apply.id;
    var state = row_apply.state;

    setVal("#assets_name_y", "");
    setVal("#sizes_y", "");
    $("#dlg_assets_check").dialog({
        title: '物资申请',
        width: 1000,
        height: 400,
        maximizable: false,
        modal: true,
        closed: true,
        buttons: [{
            id: 'submitBtn',
            text: '提交',
            iconCls: 'icon-ok',
            handler: function () {
                U.post({
                    url: getRootPath() + "/mtManage/assets/submit_apply",
                    loading: true,
                    // data: "node_seq='-1'&turn_node_seq='1'" +
                    //     "&currentuser=" + username + "&erp_no=" + dgSel.erp_no,
                    data: "apply_order_info_id=" + row_apply.id + "&apply_man=" + row_apply.apply_name + "&urgent_flag=" + row_apply.urgent_flag,
                    success: function (data) {
                        if (data.code == 200) {
                            U.msg('提交成功');
                            $("#dlg_assets_check").dialog('close');
                            queryApply();
                        } else if (data.code == 400) {//参数验证失败
                            U.msg('参数验证失败');
                        } else {
                            U.msg('服务器异常');
                        }
                    }
                });
            }
        }, {
            id: 'checkBtn',
            text: '审核',
            iconCls: 'icon-ok',
            handler: function () {
                U.post({
                    url: getRootPath() + "/mtManage/assets/check_apply",
                    loading: true,
                    data: "apply_order_info_id=" + row_apply.id + "&apply_man=" + row_apply.apply_name +
                        "&urgent_flag=" + row_apply.urgent_flag + "&currentuser=" + row_apply.currentuser +
                        "&node_seq=" + row_apply.node_seq + "&turn_node_seq=" + row_apply.turn_node_seq,
                    success: function (data) {
                        if (data.code == 200) {
                            U.msg('审核成功');
                            $("#dlg_assets_check").dialog('close');
                            queryApply();
                        } else if (data.code == 400) {//参数验证失败
                            U.msg('参数验证失败');
                        } else {
                            U.msg('服务器异常');
                        }
                    }
                });
            }
        }, {
            id: 'issueBtn',
            text: '打印',
            iconCls: 'icon-ok',
            handler: function () {
                U.post({
                    url: getRootPath() + "/mtManage/assets/issue_apply",
                    loading: true,
                    // data: {erp_no: row_apply.erp_no},
                    data: "apply_order_info_id=" + row_apply.id + "&apply_man=" + row_apply.apply_name +
                        "&urgent_flag=" + row_apply.urgent_flag + "&currentuser=" + row_apply.currentuser +
                        "&node_seq=" + row_apply.node_seq + "&turn_node_seq=" + row_apply.turn_node_seq,
                    success: function (data) {
                        if (data.code == 200) {
                            U.msg('审核成功');
                            $("#dlg_assets_check").dialog('close');
                            queryApply();
                        } else if (data.code == 400) {//参数验证失败
                            U.msg('参数验证失败');
                        } else if (data.code == 409) {
                            U.msg('用户已存在');
                        } else {
                            U.msg('服务器异常');
                        }
                    }
                });
            }
        }, {
            id: 'backBtn',
            text: '打回',
            iconCls: 'icon-ok',
            handler: function () {
                // var isValid = $("#form_apply").form('validate');
                // var getSel = $('#assets_dg_apply').datagrid('getSelections');
                // console.log("getSel:"+getSel.length)
                // if(getSel.length<1){
                //     U.msg('请选择有问题的行');
                // }
                // var isValid = $("#form_apply").form('validate');
                //getFormJson("#form_apply","deptInfo","");
                //console.log($('#form_apply'));
                // if (isValid) {
                var rs;
                $.messager.prompt("提示:", "请输入打回原因:", function (r) {
                    console.log(r)
                    rs = r;//将打回原因赋值给rs
                    if (rs == "" || rs == null) {
                        U.msg('打回失败，输入内容不能为空');
                        return;
                    } else {
                        U.post({
                            url: getRootPath() + "/mtManage/assets/back_apply",
                            loading: true,
                            data: "apply_order_info_id=" + row_apply.id + "&apply_man=" + row_apply.apply_name +
                                "&urgent_flag=" + row_apply.urgent_flag + "&currentuser=" + row_apply.currentuser +
                                "&node_seq=" + row_apply.node_seq + "&turn_node_seq=" + row_apply.turn_node_seq + "&rs=" + rs,
                            success: function (data) {
                                if (data.code == 200) {
                                    U.msg('操作成功');
                                    $("#dlg_assets_check").dialog('close');
                                    queryApply();
                                } else if (data.code == 400) {//参数验证失败
                                    U.msg('参数验证失败');
                                } else {
                                    U.msg('服务器异常');
                                }
                            }
                        });
                    }
                });
            }
        }],
        onClose: function () {
            $('#dg').datagrid('load');
        }
    });
    // $("#dlg_assets_check").dialog({
    //     title: '物资申请',
    //     width: 1000,
    //     height: 400,
    //     maximizable: false,
    //     modal: true,
    //     closed: true,
    //     buttons: [{
    //         id: 'submitBtn',
    //         text: '提交',
    //         iconCls: 'icon-ok',
    //         handler: function () {
    //             var dgSel = $('#dg').datagrid('getSelected');
    //             U.post({
    //                 url: getRootPath() + "/mtManage/assets/submit_apply",
    //                 loading: true,
    //                 // data: "node_seq='-1'&turn_node_seq='1'" +
    //                 //     "&currentuser=" + username + "&erp_no=" + dgSel.erp_no,
    //                 data:"apply_order_info_id="+dgSel.id + "&apply_man="+dgSel.apply_name+ "&urgent_flag="+dgSel.urgent_flag,
    //                 success: function (data) {
    //                     if (data.code == 200) {
    //                         U.msg('提交成功');
    //                         $("#dlg_assets_check").dialog('close');
    //                         queryApply();
    //                     } else if (data.code == 400) {//参数验证失败
    //                         U.msg('参数验证失败');
    //                     } else {
    //                         U.msg('服务器异常');
    //                     }
    //                 }
    //             });
    //         }
    //     }, {
    //         id: 'checkBtn',
    //         text: '审核',
    //         iconCls: 'icon-ok',
    //         handler: function () {
    //             var dgSel = $('#dg').datagrid('getSelected');
    //             U.post({
    //                 url: getRootPath() + "/mtManage/assets/check_apply",
    //                 loading: true,
    //                 data: "apply_order_info_id="+dgSel.id + "&apply_man="+dgSel.apply_name+
    //                     "&urgent_flag="+dgSel.urgent_flag+"&currentuser="+dgSel.currentuser+
    //                     "&node_seq="+dgSel.node_seq+"&turn_node_seq="+dgSel.turn_node_seq,
    //                 success: function (data) {
    //                     if (data.code == 200) {
    //                         U.msg('审核成功');
    //                         $("#dlg_assets_check").dialog('close');
    //                         queryApply();
    //                     } else if (data.code == 400) {//参数验证失败
    //                         U.msg('参数验证失败');
    //                     } else {
    //                         U.msg('服务器异常');
    //                     }
    //                 }
    //             });
    //         }
    //     }, {
    //         id: 'issueBtn',
    //         text: '打印',
    //         iconCls: 'icon-ok',
    //         handler: function () {
    //             var dgSel = $('#dg').datagrid('getSelected');
    //             U.post({
    //                 url: getRootPath() + "/mtManage/assets/issue_apply",
    //                 loading: true,
    //                 // data: {erp_no: row_apply.erp_no},
    //                 data: "apply_order_info_id="+dgSel.id + "&apply_man="+dgSel.apply_name+
    //                     "&urgent_flag="+dgSel.urgent_flag+"&currentuser="+dgSel.currentuser+
    //                     "&node_seq="+dgSel.node_seq+"&turn_node_seq="+dgSel.turn_node_seq,
    //                 success: function (data) {
    //                     if (data.code == 200) {
    //                         U.msg('审核成功');
    //                         $("#dlg_assets_check").dialog('close');
    //                         queryApply();
    //                     } else if (data.code == 400) {//参数验证失败
    //                         U.msg('参数验证失败');
    //                     } else if (data.code == 409) {
    //                         U.msg('用户已存在');
    //                     } else {
    //                         U.msg('服务器异常');
    //                     }
    //                 }
    //             });
    //         }
    //     }, {
    //         id: 'backBtn',
    //         text: '打回',
    //         iconCls: 'icon-ok',
    //         handler: function () {
    //             // var isValid = $("#form_apply").form('validate');
    //             // var getSel = $('#assets_dg_apply').datagrid('getSelections');
    //             // console.log("getSel:"+getSel.length)
    //             // if(getSel.length<1){
    //             //     U.msg('请选择有问题的行');
    //             // }
    //             // var isValid = $("#form_apply").form('validate');
    //             //getFormJson("#form_apply","deptInfo","");
    //             //console.log($('#form_apply'));
    //             // if (isValid) {
    //             var dgSel = $('#dg').datagrid('getSelected');
    //             var rs;
    //             $.messager.prompt("提示:", "请输入打回原因:", function (r) {
    //                 console.log(r)
    //                 rs = r;//将打回原因赋值给rs
    //                 if (rs == "" || rs == null) {
    //                     U.msg('打回失败，输入内容不能为空');
    //                     return;
    //                 } else {
    //                     U.post({
    //                         url: getRootPath() + "/mtManage/assets/back_apply",
    //                         loading: true,
    //                         data: "apply_order_info_id="+dgSel.id + "&apply_man="+dgSel.apply_name+
    //                             "&urgent_flag="+dgSel.urgent_flag+"&currentuser="+dgSel.currentuser+
    //                             "&node_seq="+dgSel.node_seq+"&turn_node_seq="+dgSel.turn_node_seq+"&rs=" + rs,
    //                         success: function (data) {
    //                             if (data.code == 200) {
    //                                 U.msg('操作成功');
    //                                 $("#dlg_assets_check").dialog('close');
    //                                 queryApply();
    //                             } else if (data.code == 400) {//参数验证失败
    //                                 U.msg('参数验证失败');
    //                             } else {
    //                                 U.msg('服务器异常');
    //                             }
    //                         }
    //                     });
    //                 }
    //             });
    //         }
    //     }],
    //     onClose: function () {
    //         $('#dg').datagrid('load');
    //     }
    // });
    // });


    //$('#applyBtn').hide();
    if ("待提交" == state) {
        $('#checkBtn').hide();
        $('#backBtn').hide();
        $('#issueBtn').hide();
    }
    if (row_apply.apply_name != username) {
        $('#submitBtn').hide();
    }
    if ("审核" == state) {
        $('#issueBtn').hide();
        $('#submitBtn').hide();
    }
    if (row_apply.currentuser != username) {
        $('#checkBtn').hide();
        $('#backBtn').hide();
    }
    if ("打印" == state) {
        $('#checkBtn').hide();
        $('#backBtn').hide();
        $('#submitBtn').hide();
    }
    if (row_apply.currentuser != username) {
        $('#issueBtn').hide();
    }
    if ("打回" == state) {
        $('#checkBtn').hide();
        $('#backBtn').hide();
        $('#issueBtn').hide();
    }
    if (row_apply.apply_name != username) {
        $('#submitBtn').hide();
    }
    if ("已打印" == state) {
        $('#checkBtn').hide();
        $('#backBtn').hide();
        $('#issueBtn').hide();
        $('#submitBtn').hide();
    }
    //涉及平行审核的时候需要用下代码进行判断按钮，代码需修改确认
    // U.post({
    //     url: getRootPath() + "/mtManage/assets/judge_permission",
    //     loading: true,
    //     data: {
    //         id: id,
    //         username: username,
    //         state: row_apply.state
    //     },
    //     success: function (data) {
    //         if (data.code == 200) {
    //             if (!data.data) {
    //                 if ("打印" == state) {
    //                     $('#issueBtn').hide();
    //                 } else if ("审核" == state) {
    //                     $('#checkBtn').hide();
    //                     $('#backBtn').hide();
    //                     //$('#issueBtn').hide();
    //                 } else if ("已打印" == state) {
    //                     $('#issueBtn').hide();
    //                 }
    //             }
    //         } else if (data.code == 400) {//参数验证失败
    //             U.msg('参数验证失败');
    //         } else {
    //             U.msg('服务器异常');
    //         }
    //     }
    // });
    //审核dialog中的datagrid
    $('#assets_dg_apply').datagrid({
        method: "get",
        url: getRootPath() + '/mtManage/assets/list_applyByErp',
        fit: true,
        border: true,
        // idField: 'apply_id',
        striped: true,
        pagination: true,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        checkOnSelect: true,
        fitColumns: true,
        toolbar: '#tb_apply',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'apply_id', title: 'apply_id', hidden: true},
            {field: 'erp_no', title: 'ERP', width: 100},
            // {field: 'apply_dept', title: '部门', width: 100},
            {field: 'apply_man', title: '申请人', width: 100},
            // {field: 'return_plandate', title: '预计归还',  width: 100},
            {field: 'apply_time', title: '申请时间', width: 100},
            {field: 'state', title: '状态', width: 100},
            {field: 'currentuser', title: '当前责任人', width: 100},
            // {field: 'dept_name', title: '物资部门', width: 100},
            // {field: 'type_main_name', title: '物资类别', width: 100},
            {field: 'type_dtl_name', title: '物资名称', width: 100},
            // {field: 'type_dtl_no', title: '物资编号', width: 100},
            {field: 'model', title: '型号', width: 100},
            {field: 'sizes', title: '规格', width: 100},
            // {field: 'out_type', title: '出库类型', width: 100},
            {field: 'apply_num', title: '申请数量', width: 100},
            // {field: 'backto_user', title: '打回人', width: 100},
            // {field: 'backto_time', title: '打回时间', width: 100},
            // {field: 'besureout_user', title: '发放人',  width: 100},
            // {field: 'besureout_time', title: '发放时间',  width: 100},
            // {field: 'refusereason_user', title: '拒绝发放人',  width: 100},
            // {field: 'refusereason', title: '拒绝发放原因',  width: 100},
            // {field: 'operator_record', title: '操作记录', width: 100},
            {field: 'apply_reason', title: '申请原因', width: 100},
            {field: 'stock_num', title: '库存数量', width: 100},
            {field: 'demand_time', title: '需求日期', width: 100},
            {
                field: 'remark', title: '备注', width: 100,
                formatter: function (value, row, index) {
                    if (value == null) {
                        value = "";
                    } else {
                        return "<span title='" + value + "'>" + value + "</span>";
                    }
                }
            },
            {field: 'for_workshop', title: '使用车间', hidden: true},
            {field: 'dept_use', title: '投入部门', width: 100}
            // {
            //     field: 'operate', title: '操作', width: 100,
            //     formatter: function (value, row, index) {
            //         var d = "<a onclick='remove_flow(" + row.id + ")' class='button-delete button-red'>删除</a>";
            //         var e = "<a onclick='edit_flow(" + row.id + ")' class='button-edit button-blue'>编辑</a>";
            //         var r = "<a onclick='getOperator(" + row.id + ")' class='button-edit button-teal'>操作员配置</a>";
            //         if (row.serial_flag == '2') {//物资申请
            //             return e + '  ' + d;
            //         } else {
            //             return e + '  ' + d + '  ' + r;
            //         }
            //     }
            //
            // }
        ]],
        onLoadSuccess: function (data) {
            $('#assets_dg_apply').datagrid("selectRow", 0);
        },
        queryParams: {
            "erp_no": row_apply.erp_no,
            "apply_man": row_apply.apply_name,
            "apply_order_info_id": row_apply.id
        },
        onDblClickRow: function (index, row) {
            // setVal("#type_main_name", row.type_main_name);
            // $("#apply_id").val(row.apply_id);
            //修改物资申请
            operator_apply_info(row_apply);
        }
    });

    $("#btn-addByErpno").show();
    $("#btn-editByErpno").show();
    $("#btn-deleteByErpno").show();
    if ((row_apply.state != '待提交' && row_apply.state != '打回') || row_apply.apply_name != username) {
        $("#btn-addByErpno").hide();
        $("#btn-editByErpno").hide();
        $("#btn-deleteByErpno").hide();
    }

    $("#dlg_assets_check").dialog('open');

    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    //setVal("#operator_info",node_name);
    //$("#operator_node").val(id);
}

function operator_apply_info(row) {
    var row_apply = $('#assets_dg_apply').datagrid('getSelected');
    console.log(row_apply);
    if (row_apply == null) {
        U.msg('请先选中');
        return;
    }
    // var row = datagrid.datagrid('getSelected');
    var id = row_apply.apply_id;
    var state = row_apply.state;
    var apply_man = row_apply.apply_man;
    var currentuser = row_apply.currentuser;
    var node_seq = row_apply.node_seq;
    var turn_node_seq = row_apply.turn_node_seq;

    dialog_apply_form = $("#dlg_apply_form").dialog({
        title: '物资申请明细',
        width: 680,
        height: 500,
        maximizable: false,
        modal: true,
        closed: true,
        buttons: [{
            id: "confirm_apply",
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_apply").form('validate');
                //getFormJson("#form_apply","deptInfo","");
                //console.log($('#form_apply'));
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/add_apply",
                        loading: true,
                        data: $('#form_apply').serialize(),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialog_apply_form.dialog('close');
                                $('#assets_dg_apply').datagrid('load');
                                $(datagrid).datagrid('load');
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('用户已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialog_apply_form.dialog('close');
            }
        }]
    });
    setVal("#state", row_apply.state);
    setVal("#dept_name", row_apply.dept_name);
    setVal("#apply_dept", row_apply.apply_dept);
    setVal("#type_main_name", row_apply.type_main_name);
    setVal("#type_dtl_name", row_apply.type_dtl_name);
    setVal("#type_dtl_no", row_apply.type_dtl_no);
    setVal("#model", row_apply.model);
    setVal("#sizes", row_apply.sizes);
    setVal("#return_plandate", new Date(row_apply.return_plandate).format('yyyy-MM-dd'));
    setVal("#apply_num", row_apply.apply_num);
    setVal("#apply_man", apply_man);
    setVal("#apply_time", new Date(row_apply.apply_time).format('yyyy-MM-dd'));
    setVal("#currentuser", currentuser);
    setVal("#backto_user", row_apply.backto_user);
    setVal("#backto_time", new Date(row_apply.backto_time).format('yyyy-MM-dd'));
    setVal("#besureout_user", row_apply.besureout_user);
    setVal("#besureout_time", row_apply.besureout_time);
    setVal("#refusereason", row_apply.refusereason);
    setVal("#operator_record", row_apply.operator_record);
    setVal("#demand_time", new Date(row_apply.demand_time).format('yyyy-MM-dd'));
    setVal("#stock_num", row_apply.stock_num);
    setVal("#apply_reason", row_apply.apply_reason);
    setVal("#remark", row_apply.remark);
    setVal("#dept_use", row_apply.dept_use);
    setVal("#use_to", row_apply.use_to);
    setVal("#for_workshop",row_apply.for_workshop);
    if (row_apply.validation_flag == '1') {
        setVal("#validation_flag", "已承认");
    } else if (row_apply.validation_flag == '0') {
        setVal("#validation_flag", "未承认");
    } else {
        setVal("#validation_flag", "");
    }
    $("#assets_id").val(row_apply.assets_id);
    $("#apply_id").val(id);
    $("#out_type").combobox('setValue', row_apply.out_type);
    $("#erpInfo").combobox('setValue', row_apply.erp_no);
    $("#generic_flag").combobox('setValue', row_apply.generic_flag);

    $("#confirm_apply").hide();
    if (row_apply.urgent_flag == '1') {
        $("#urgent_flag_id").combobox('setValue', "1");
    } else {
        $("#urgent_flag_id").combobox('setValue', "0");
    }
    dialog_apply_form.dialog("open");
}

function edit_operator() {
    var row = operatorDataGrid.datagrid('getSelected');
    if (row == null) {
        U.msg('请先选中');
        return;
    }

    var row_flow = flowDataGrid.datagrid('getSelected');
    if (row_flow == null) {
        return;
    }
    var node_seq = row_flow.node_seq;
    var node_name = row_flow.node_name;
    $.getJSON(getRootPath() + "/mtManage/assets/list_dept", function (json) {
        $('#deptInfo').combobox({
            data: json.data, //获取到的json 数据
            valueField: "dept_id",
            textField: "dept_name"
        });
        $("#deptInfo").combobox('setValue', row.dept_id);
    });

    $.getJSON(getRootPath() + "/mtManage/assets/list_user", function (json) {
        $('#userInfo').combobox({
            data: json.data, //获取到的json 数据
            valueField: "user_id",
            textField: "user_name"
        });
        $("#userInfo").combobox('setText', row.user_name);
    });
    dialog_operator_form = $("#dlg_operator_form").dialog({
        title: '添加操作员信息',
        width: 340,
        height: 300,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_operator").form('validate');
                var dept_id = $("#deptInfo").combobox("getValue");
                var dept_name = $("#deptInfo").combobox("getText");
                var user_id = $("#userInfo").combobox("getValue");
                var user_name = $("#userInfo").combobox("getText");
                var operator_id = row.id;
                var addObj = [{"name": "dept_id", "value": dept_id},
                    {"name": "dept_name", "value": dept_name},
                    {"name": "user_id", "value": user_id},
                    {"name": "user_name", "value": user_name},
                    {"name": "id", "value": operator_id}
                ];
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/mtManage/assets/update_operator",
                        loading: true,
                        data: getFormJson("#form_operator", "deptInfo", addObj),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                dialog_operator_form.dialog('close');
                                queryOperator();
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('已存在');
                            } else {
                                U.msg('服务器异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialog_operator_form.dialog('close');
            }
        }]
    });
    console.log(node_name);
    console.log(node_seq);
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    setVal("#operator_info", node_name);
    $("#operator_node").val(node_seq);
    $('#sequence').numberspinner('setValue', row.sequence);

}

function remove_operator(id) {
    var row = operatorDataGrid.datagrid('getSelected');
    if (row == null) {
        U.msg('请先选中');
        return;
    } else {
        id = row.id;
    }

    parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function (data) {
        if (data) {
            U.post({
                url: getRootPath() + "/mtManage/assets/del_operator",
                loading: true,
                data: {id: id},
                success: function (data) {
                    var flag = data.data;
                    if (flag == "1") {
                        U.msg('删除成功');
                        queryOperator();
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 404) {
                        U.msg('未找到该用户');
                    } else {
                        U.msg('服务器异常');
                    }
                }
            });
        }
    });
}

function remove_flow(id) {
    var row = flowDataGrid.datagrid('getSelected');
    if (row == null) {
        U.msg('请先选中');
        return;
    } else {
        id = row.id;
    }

    parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function (data) {
        if (data) {
            U.post({
                url: getRootPath() + "/mtManage/assets/del_flow",
                loading: true,
                data: {id: id},
                success: function (data) {
                    if (data.code == 200) {
                        U.msg('删除成功');
                        queryFlow();
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 404) {
                        U.msg('未找到该用户');
                    } else {
                        U.msg('服务器异常');
                    }
                }
            });
        }
    });
}

function remove_apply() {
    var row = datagrid.datagrid('getSelections');
    if (row == null) {
        U.msg('请先选中');
        return;
    } else if (row.length > 1) {
        U.msg('请单条删除');
        return;
    } else {
        erp_no = row[0].erp_no;
    }
    // var cheack_falg = edit_apply_cheack(row.currentuser, row.state,"");
    if (row[0].state == "待提交" && row[0].apply_name == username) {

        parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function (data) {
            if (data) {
                U.post({
                    url: getRootPath() + "/mtManage/assets/del_apply",
                    loading: true,
                    data: {id: row[0].id},
                    success: function (data) {
                        if (data.code == 200) {
                            U.msg('删除成功');
                            row.length = 0;
                            queryApply();
                        } else if (data.code == 400) {//参数验证失败
                            U.msg('参数验证失败');
                        } else if (data.code == 404) {
                            U.msg('未找到该用户');
                        } else {
                            U.msg('服务器异常');
                        }
                    }
                });
            }
        });
    } else {
        U.msg("当前流程已审核，不能进行修改或删除")
    }

}

//单条删除
function remove_applyByOne() {
    var row = $('#assets_dg_apply').datagrid('getSelected');
    if (row == null) {
        U.msg('请先选中');
        return;
    }
    // var cheack_falg = edit_apply_cheack(row.currentuser, row.state, row.apply_man);
    parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function (data) {
        if (data) {
            U.post({
                url: getRootPath() + "/mtManage/assets/del_applyByOne",
                loading: true,
                data: {apply_id: row.apply_id, apply_order_info_id: row.apply_order_info_id},
                success: function (data) {
                    if (data.code == 200) {
                        U.msg('删除成功');
                        $('#assets_dg_apply').datagrid('load');
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 404) {
                        U.msg('未找到该用户');
                    } else {
                        U.msg('服务器异常');
                    }
                }
            });
        }
    });
}

/**操作员配置列表*/
function getOperator() {
    var row = flowDataGrid.datagrid('getSelected');
    var node_seq;
    if (row == null) {
        U.msg('请先选择流程');
        return
    } else {
        node_seq = row.node_seq;
    }

    dialog_operator = $("#dlg_operator").dialog({
        title: '节点人员配置管理',
        width: 700,
        height: 400,
        maximizable: false,
        modal: true,
        // buttons: [{
        //     text: '确认',
        //     iconCls: 'icon-ok',
        //     handler: function () {
        //         U.post({
        //             url: getRootPath()+'/sys/user/' + id + '/role/modify',
        //             loading: true,
        //             data: {
        //                 roles: getSelectedRoles()
        //             },
        //             success: function (data) {
        //                 if (data.code == 200) {
        //                     U.msg('修改成功');
        //                     dialog_operator.dialog('close');
        //                     queryOperator();
        //                 } else if (data.code == 400) {//参数验证失败
        //                     U.msg('参数验证失败');
        //                 } else if (data.code == 404) {
        //                     U.msg('未找到该用户');
        //                 } else {
        //                     U.msg('服务器异常');
        //                 }
        //             }
        //         });
        //     }
        // }, {
        //     text: '取消',
        //     handler: function () {
        //         dialog_operator.dialog('close');
        //     }
        // }]
    });

    operatorDataGrid = $('#operator-dg').datagrid({
        method: "post",
        url: getRootPath() + '/mtManage/assets/list_operator?node_seq=' + node_seq,
        fit: true,
        fitColumns: true,
        border: true,
        idField: 'id',
        striped: true,
        pagination: false,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        toolbar: '#bar-operator',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', title: 'id', hidden: true},
            {field: 'dept_name', title: '部门', hidden: true},
            {field: 'dept_id', title: '部门ID', hidden: true},
            {field: 'operator_node', title: '节点ID', hidden: true},
            {field: 'operator_info', title: '节点名称', sortable: false, width: 25},
            {field: 'sequence', title: '排序', sortable: false, width: 25},
            {field: 'user_name', title: '操作员', sortable: false, width: 35},
            {
                field: 'operate', title: '操作', width: 100,
                formatter: function (value, row, index) {
                    var d = "<a onclick='remove_operator(" + row.id + ")' class='button-delete button-red'>删除</a>";
                    var e = "<a onclick='edit_operator(" + row.id + ")' class='button-edit button-blue'>编辑</a>";
                    return e + '  ' + d;

                }

            }
        ]],
        onLoadSuccess: function (data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});
        }
    });

}

function getFlow() {
    dialog_flow = $("#dlg").dialog({
        title: '流程模板管理',
        width: 800,
        height: 400,
        maximizable: false,
        modal: true,
        // buttons: [{
        //     text: '确认',
        //     iconCls: 'icon-ok',
        //     handler: function () {
        //         U.post({
        //             url: getRootPath()+'/sys/user/' + id + '/role/modify',
        //             loading: true,
        //             data: {
        //                 roles: getSelectedRoles()
        //             },
        //             success: function (data) {
        //                 if (data.code == 200) {
        //                     U.msg('修改成功');
        //                     dialog_flow.dialog('close');
        //                     queryFlow();
        //                 } else if (data.code == 400) {//参数验证失败
        //                     U.msg('参数验证失败');
        //                 } else if (data.code == 404) {
        //                     U.msg('未找到该用户');
        //                 } else {
        //                     U.msg('服务器异常');
        //                 }
        //             }
        //         });
        //     }
        // }, {
        //     text: '取消',
        //     handler: function () {
        //         dialog_flow.dialog('close');
        //     }
        // }]
    });

    flowDataGrid = $('#flow-dg').datagrid({
        method: "post",
        url: getRootPath() + '/mtManage/assets/list_flow',
        fit: true,
        fitColumns: true,
        border: true,
        idField: 'id',
        striped: true,
        pagination: false,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        toolbar: '#bar-flow',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'id', title: 'id', hidden: true},
            {field: 'model_name', title: '模板名称', sortable: false, width: 25},
            {field: 'node_name', title: '节点名称', sortable: false, width: 25},
            {field: 'node_seq', title: '节点序号', hidden: true},
            {field: 'turn_node_seq', title: '转向节点序号', hidden: true},
            {
                field: 'serial_flag', title: '是否并行操作', sortable: false, width: 50,
                formatter: function (value, row, index) {
                    if (value == '2') {
                        return '';
                    } else if (value == '1') {
                        return '是';
                    } else {
                        return '否';
                    }
                }
            }
            // {
            //     field: 'operate', title: '操作', width: 100,
            //     formatter: function (value, row, index) {
            //         var d = "<a onclick='remove_flow(" + row.id + ")' class='button-delete button-red'>删除</a>";
            //         var e = "<a onclick='edit_flow(" + row.id + ")' class='button-edit button-blue'>编辑</a>";
            //         var r = "<a onclick='getOperator(" + row.id + ")' class='button-edit button-teal'>操作员配置</a>";
            //         if (row.serial_flag == '2') {//物资申请
            //             return e + '  ' + d;
            //         } else {
            //             return e + '  ' + d + '  ' + r;
            //         }
            //     }
            //
            // }
        ]]
        // onLoadSuccess: function (data) {
        //     $('.button-delete').linkbutton({});
        //     $('.button-edit').linkbutton({});
        // }
    });
}

function getAssets() {
    setVal("#k3_code_dlg", "");
    setVal("#assets_name", "");
    dialog_assets = $("#dlg_assets").dialog({
        title: '选择物资',
        width: 1000,
        height: 550,
        maximizable: false,
        modal: true
    });

    assetsDataGrid = $('#assets-dg').datagrid({
        method: "post",
        url: getRootPath() + '/mtManage/assets/list_Detail',
        fit: true,
        fitColumns: true,
        border: true,
        idField: 'type_dtl_id',
        striped: true,
        pagination: true,
        rownumbers: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 50, 100],
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'type_dtl_id', title: 'id', hidden: true},
            {field: 'type_dtl_name', title: '物资名称', sortable: false, width: 25},
            {field: 'k3_code', title: 'K3编码', sortable: false, width: 25},
            {field: 'type_dtl_no', title: '物资编号', sortable: false, width: 25},
            {field: 'model', title: '物资型号', sortable: false, width: 25},
            {field: 'sizes', title: '物资规格', sortable: false, width: 25},
            {field: 'type_main_id', title: '主分类id', hidden: true},
            {field: 'dept_name', title: '部门', hidden: true},
            {field: 'type_main_name', title: '分类名称', sortable: false, width: 25}
            // {
            //     field: 'operate', title: '操作', width: 100,
            //     formatter: function (value, row, index) {
            //         var d = "<a onclick='remove_flow(" + row.id + ")' class='button-delete button-red'>删除</a>";
            //         var e = "<a onclick='edit_flow(" + row.id + ")' class='button-edit button-blue'>编辑</a>";
            //         var r = "<a onclick='getOperator(" + row.id + ")' class='button-edit button-teal'>操作员配置</a>";
            //         if (row.serial_flag == '2') {//物资申请
            //             return e + '  ' + d;
            //         } else {
            //             return e + '  ' + d + '  ' + r;
            //         }
            //     }
            //
            // }
        ]],
        // onLoadSuccess: function (data) {
        //     $('.button-delete').linkbutton({});
        //     $('.button-edit').linkbutton({});
        // },
        queryParams: {
            "typeMainId": ''
        },
        onDblClickRow: function (index, row) {
            console.log("双击执行！")
            setVal("#dept_name", row.dept_name);
            setVal("#type_dtl_name", row.type_dtl_name);
            setVal("#type_dtl_no", row.type_dtl_no);
            setVal("#model", row.model);
            setVal("#sizes", row.sizes);
            setVal("#type_main_name", row.type_main_name);
            setVal("#supplier", row.supplier);
            if (row.validation_flag == '1') {
                setVal("#validation_flag", "已承认");
            } else if (row.validation_flag == '0') {
                setVal("#validation_flag", "未承认");
            } else {
                setVal("#validation_flag", "");
            }
            setVal("#supplier", row.supplier);
            $("#assets_id").val(row.type_dtl_id);
            //对库存数量进行赋值
            U.post({
                url: getRootPath() + "/mtManage/assets/getStockinNum",
                loading: true,
                data: {
                    type_dtl_no: row.type_dtl_no
                },
                success: function (data) {
                    if (data.code == 200) {
                        var stockinNum = data.data[0];
                        if (stockinNum == null) {
                            setVal("#stock_num", 0);
                        } else {
                            setVal("#stock_num", stockinNum.stockin_num);
                        }
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 409) {
                        U.msg('');
                    } else {
                        U.msg('服务器异常');
                    }
                }
            });
            dialog_assets.dialog('close');
        }
    });
}

//字符串前面自动补0
function PrefixInteger(num, m) {
    return (Array(m).join(0) + num).slice(-m);
}

//保存用户角色
function getSelectedRoles() {

    //所选的角色列表
    var roleIds = [];
    var data = $('#flow-dg').datagrid('getSelections');
    for (var i = 0, j = data.length; i < j; i++) {
        roleIds.push(data[i].id);
    }
    return roleIds;
}

function printBill() {
    var row_apply = $('#assets_dg_apply').datagrid('getSelected');
    var erpInfo;
    var nw;
    var conProductNum = "";
    var sdOrderAmount = "";
    var cusSentToName = "";
    var conDept = "";
    parent.$.messager.confirm('提示', '确认后系统将记录此单据已打印', function (data) {
        if (data) {
            $.ajax({
                type: 'post',
                url: getRootPath() + "/mtManage/assets/insertPrintRecord",
                data: {
                    apply_info_order_id: row_apply.apply_order_info_id
                },
                success: function (data) {
                    if (data.code == 200) {
                        $.ajax({
                            type: 'post',
                            url: getRootPath() + "/mtManage/assets/getErpInfoForPrint",
                            data: {
                                apply_order_info_id: row_apply.apply_order_info_id
                            },
                            success: function (data) {
                                if (data.code == 200) {
                                    erpInfo = data.data[0];
                                    var trString = "";
                                    var SerialNumber = 1;
                                    var apply_man = "";
                                    var deptUse = "";
                                    var urgentTrString = "";
                                    if (erpInfo.urgent_flag == 1) {
                                        urgentTrString = "   <span style=\"margin-left:150px;\">状态:"
                                            + "   <input type=\"checkbox\" name=\"urgent_flag\" checked=\"true\" value=\"1\" style=\"margin-left:50px;\" />加急"
                                            + "   <input type=\"checkbox\" name=\"urgent_flag\"  value=\"0\" style=\"margin-left:50px;\"/>正常"
                                            + "   </span>";
                                    } else {
                                        urgentTrString = "   <span style=\"margin-left:150px;\">状态:"
                                            + "   <input type=\"checkbox\" name=\"urgent_flag\"  value=\"1\" style=\"margin-left:50px;\"/>加急"
                                            + "   <input type=\"checkbox\" name=\"urgent_flag\" checked=\"true\" value=\"0\" style=\"margin-left:50px;\"/>正常"
                                            + "   </span>";
                                    }
                                    data.data.forEach(function (info) {
                                        var conProductNum = info.conProductNum;
                                        var sdOrderAmount = info.sdOrderAmount;
                                        var cusSentToName = info.cusSentToName;
                                        var conDept = info.conDept;
                                        deptUse = deptUse + info.dept_use + " ";
                                        apply_man = apply_man + info.apply_man + " ";
                                        if(typeof info.for_workshop === 'object'){
                                            info.for_workshop = ""
                                        }
                                        if (info.erp_no == "") {
                                            trString = trString
                                                + "    <tr style=\"font-size:10px;\">"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + SerialNumber + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_no + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_name + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>"+info.for_workshop+"</td>"//使用车间
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_num + info.unit +"</td>"
                                                + "     <td align=\"center\" style=\"text-align:left;word-wrap:break-word\">" + info.sizes.replace(/\n|\r\n/g, "<br>") + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'></td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.use_to.replace(/\n|\r\n/g, "<br>")
                                                + "    </td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_reason + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + new Date(info.demand_time).format('yyyy-MM-dd') + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.stock_num + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.remark + "</td>"
                                                + "    </tr>";
                                        } else {
                                            trString = trString
                                                + "    <tr style=\"font-size:10px;\">"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + SerialNumber + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_no + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_name + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>"+info.for_workshop+"</td>"//使用车间
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_num + info.unit + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.sizes + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'></td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>订单号:" + info.erp_no + "</br>"
                                                + "     机型:" + conProductNum + "</br>"
                                                + "     数量:" + sdOrderAmount + "</br>"
                                                + "     发往地:" + cusSentToName + "</br>"
                                                + "     事业部:" + conDept
                                                + "    </td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_reason + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + new Date(info.demand_time).format('yyyy-MM-dd') + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.stock_num + "</td>"
                                                + "     <td align=\"center\" style='word-wrap:break-word'>" + info.remark + "</td>"
                                                + "    </tr>";
                                        }
                                        SerialNumber++;
                                    })

                                    var strArr = apply_man.split(" ");//把字符串分割成一个数组
                                    strArr.sort();//排序
                                    var result = new Array();//创建出一个结果数组
                                    var tempStr = "";
                                    for (var i in strArr) {
                                        if (strArr[i] != tempStr) {
                                            result.push(strArr[i]);
                                            tempStr = strArr[i];
                                        } else {
                                            continue;
                                        }
                                    }
                                    apply_man = result.join(" ");

                                    //使用部门
                                    var deptStrArr = deptUse.split(" ");//把字符串分割成一个数组
                                    deptStrArr.sort();//排序
                                    var deptResult = new Array();//创建出一个结果数组
                                    var tempDeptStr = "";
                                    for (var j in deptStrArr) {
                                        if (deptStrArr[j] != tempDeptStr) {
                                            deptResult.push(deptStrArr[j]);
                                            tempDeptStr = deptStrArr[j];
                                        } else {
                                            continue;
                                        }
                                    }
                                    deptUse = deptResult.join(" ");

                                    var serialNum = PrefixInteger(row_apply.apply_order_info_id, 6);

                                    nw = window.open();
                                    //将表格放在div里，然后把整个div里面的内容给赋给一个变量。
                                    var tab_html = '<div id="sendTitle" style="width:1150px;margin:0 auto;height:30px;"><img style="width:420px;height:60px;" src="/psystem/static/images/tablelogo.png"/></div>'
                                        + "<div style=\"width:1150px;margin:0 auto\">"
                                        + "     <p align=\"center\" style=\"font-size:40px;font-weight: bold;\">请购单</p>"
                                        + "  <div width=\"1150px\" style=\"font-size:20px;\" style=\"margin-bottom:10px;\">"
                                        + "    <span style=\"margin-right:30px;\">申购部门:" + row_apply.apply_dept + "</span>"
                                        + "    <span style=\"margin-right:30px;\">投入部门:" + deptUse + "</span>"
                                        + "    <span style=\"margin-right:30px;\">申购人:" + apply_man + "</span>"
                                        + "   <span style=\"margin-right:20px;\">申请日期:" + new Date(row_apply.apply_time).format('yyyy-MM-dd') + "</span>"
                                        + "   <span style=\"margin-right:20px;\">表单序号:" + new Date(row_apply.apply_time).format('yyyyMMdd') + serialNum + "</span>"
                                        + "  </div>"
                                        + "  <div width=\"1150px\" style=\"font-size:20px;\">"
                                        + "   <span>申购类型:"
                                        + "   <input type=\"checkbox\" name=\"temp\" value=\"临时\" />临时"
                                        + "   <input type=\"checkbox\" name=\"new\" checked=\"true\" value=\"新增\" style=\"margin-left:50px;\"/>新增"
                                        + "   <input type=\"checkbox\" name=\"special\" value=\"订单特殊需求\" style=\"margin-left:50px;\" />订单特殊需求"
                                        + "   <input type=\"checkbox\" name=\"temp\" value=\"外部维修\" style=\"margin-left:50px;\" />外部维修"
                                        + "   </span>"
                                        + urgentTrString
                                        + " </div>"
                                        + "  </div>"
                                        + "  <table border=\"1\" cellspacing=\"0\" width=\"1150px\"  style=\"margin:0 auto;table-layout:fixed\">"
                                        + "   <tbody>"
                                        + "    <tr style=\"font-size:15px;\">"
                                        + "     <th align=\"center\" width=\"20px;\">序号</th>"
                                        + "     <th align=\"center\" width=\"70px;\">物资编号</th>"
                                        + "     <th align=\"center\" width=\"90px;\">物品名称</th>"
                                        + "     <th align=\"center\" width=\"40px;\">使用车间</th>"
                                        + "     <th align=\"center\" width=\"30px;\">数量/单位</th>"
                                        + "     <th align=\"center\" width=\"170px;\">规格</th>"
                                        + "     <th align=\"center\" width=\"20px;\">单价</th>"
                                        + "     <th align=\"center\" width=\"170px;\">用途</th>"
                                        + "     <th align=\"center\" width=\"180px;\">申请原因</th>"
                                        + "     <th align=\"center\" >需求日期</th>"
                                        + "     <th align=\"center\" width=\"35px;\">确认库存数量</th>"
                                        + "     <th align=\"center\" width=\"240px;\">备注</th>"
                                        + "    </tr>" +

                                        // + "    <tr style=\"font-size:15px;\">"
                                        // + "     <th align=\"center\" >序号</th>"
                                        // + "     <th align=\"center\" >物资编号</th>"
                                        // + "     <th align=\"center\" >物品名称</th>"
                                        // + "     <th align=\"center\" >数量/单位</th>"
                                        // + "     <th align=\"center\" >规格</th>"
                                        // + "     <th align=\"center\" >单价</th>"
                                        // + "     <th align=\"center\" >用途</th>"
                                        // + "     <th align=\"center\" >申请原因</th>"
                                        // + "     <th align=\"center\">需求日期</th>"
                                        // + "     <th align=\"center\" >确认库存数量</th>"
                                        // + "     <th align=\"center\">备注</th>"
                                        // + "    </tr>" +

                                        // + "    <tr id=\"item01\">"
                                        trString
                                        + " <tr height=\"150px\">"
                                        + " <td colspan=\"12\" style=\"text-align:left;vertical-align:top;\">具体要求:</td>"
                                        + "</tr>"
                                        + "<tr height=\"40px\">"
                                        + "  <td colspan=\"5\">预算资金:</td>"
                                        + "  <td colspan=\"3\">结算资金:</td>"
                                        + "  <td colspan=\"4\">费用归属中心（部门）审核:</td>"
                                        + "</tr>"
                                        + "<tr height=\"40px\">"
                                        + "<td colspan=\"5\">部门经理审核:</td>"
                                        + "<td colspan=\"3\">副总经理审核:</td>"
                                        + "<td colspan=\"4\">总经理审核:</td>"
                                        + "</tr>"
                                        + "<tr height=\"40px\">"
                                        + "  <td colspan=\"5\">报价初核:</td>"
                                        + "  <td colspan=\"7\">报价批准:</td>"
                                        + "</tr>"
                                        + "</tbody>"
                                        + "</table>"
                                        + "<div style=\"width:1150px;margin:0 auto;\">"
                                        + "<p>说明：签名时，请签上日期。</p>"
                                        + "</div>"
                                        + "<div style=\"width:1150px;margin:0 auto;\">"
                                        + " <p align=\"right\" style=\"height:10px;\">表单编号：UM-FM-ED-175B</p>"
                                        + " <p align=\"right\" style=\"height:10px;align:right;\">保存期限：3年</p>"
                                        + "</div>";
                                    var tit_html = '<div id="sendTitle" style="width:1150px;"><center><span style="font-size:24px"><b></b></span></center></div>';
                                    tab_html = tab_html.replace(/disabled/g, "");
                                    tab_html = tab_html.replace(/<TEXTAREA/g, '<TEXTAREA readonly style="border:0px 0px 0px 0px;"');
                                    tab_html = tab_html.replace(/<INPUT/g, '<INPUT readonly style="border:0px 0px 0px 0px;" ');
                                    tab_html = tab_html.replace(/type=checkbox/g, 'type=checkbox onClick="return false;"');
                                    tab_html = tab_html.replace(/type=button/g, 'type=button style="visibility:hidden"');
                                    //console.log(tab_html);
                                    //document.getElementsByTagName('body')[0].style.zoom=0.92;
                                    nw.document.open("text/html", "GB2312");
                                    nw.document.write("<style media=print>.Noprint{display:none;}.PageNext{page-break-BEFORE: always;} html{zoom:80%;}</style>");
                                    nw.document.write(tit_html);//打印第一个表格
                                    nw.document.write(tab_html);//打印第一个表格
                                    nw.document.write('<div  style="margin:0 auto;width:100px;"><input type="button" class="Noprint" onclick = "javascript:window.print()" value="确定打印" /></div>');
                                    //nw.location.reload();
                                    //nw.document.close();
                                }
                            },
                            complete: function () {

                            }
                        });
                    } else {
                        U.msg("数据更新出错，打印信息未更新")
                        return;
                    }
                },
                complete: function () {

                }
            });
        }
    });


    /*document.body.innerHTML=document.getElementById('dlg_apply_form').innerHTML;
    window.print();*/
}

function printBillBefore() {
    var row_apply = $('#assets_dg_apply').datagrid('getSelected');
    var erpInfo;
    var nw;
    var conProductNum = "";
    var sdOrderAmount = "";
    var cusSentToName = "";
    var conDept = "";
    $.ajax({
        type: 'post',
        url: getRootPath() + "/mtManage/assets/getErpInfoForPrint",
        data: {
            apply_order_info_id: row_apply.apply_order_info_id
        },
        success: function (data) {
            if (data.code == 200) {
                erpInfo = data.data[0];
                var trString = "";
                var SerialNumber = 1;
                var apply_man = "";
                var deptUse = "";
                var urgentTrString = "";
                if (erpInfo.urgent_flag == 1) {
                    urgentTrString = "   <span style=\"margin-left:150px;\">状态:"
                        + "   <input type=\"checkbox\" name=\"urgent_flag\" checked=\"true\" value=\"1\" style=\"margin-left:50px;\" />加急"
                        + "   <input type=\"checkbox\" name=\"urgent_flag\"  value=\"0\" style=\"margin-left:50px;\"/>正常"
                        + "   </span>";
                } else {
                    urgentTrString = "   <span style=\"margin-left:150px;\">状态:"
                        + "   <input type=\"checkbox\" name=\"urgent_flag\"  value=\"1\" style=\"margin-left:50px;\"/>加急"
                        + "   <input type=\"checkbox\" name=\"urgent_flag\" checked=\"true\" value=\"0\" style=\"margin-left:50px;\"/>正常"
                        + "   </span>";
                }
                data.data.forEach(function (info) {
                    var conProductNum = info.conProductNum;
                    var sdOrderAmount = info.sdOrderAmount;
                    var cusSentToName = info.cusSentToName;
                    var conDept = info.conDept;
                    deptUse = deptUse + info.dept_use + " ";
                    apply_man = apply_man + info.apply_man + " ";
                    if(typeof info.for_workshop === 'object'){
                        info.for_workshop = ""
                    }
                    if (info.erp_no == "") {
                        trString = trString
                            + "    <tr style=\"font-size:10px;\">"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + SerialNumber + "</td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_no + "</td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_name + "</td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>"+info.for_workshop+"</td>"//使用车间
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_num + info.unit +"</td>"
                            + "     <td align=\"center\" style=\"text-align:left;word-wrap:break-word\">" + info.sizes.replace(/\n|\r\n/g, "<br>") + "</td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'></td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.use_to.replace(/\n|\r\n/g, "<br>")
                            + "    </td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_reason + "</td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + new Date(info.demand_time).format('yyyy-MM-dd') + "</td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.stock_num + "</td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.remark + "</td>"
                            + "    </tr>";
                    } else {
                        trString = trString
                            + "    <tr style=\"font-size:10px;\">"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + SerialNumber + "</td>"//序号
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_no + "</td>"//物资编号
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.type_dtl_name + "</td>"//物品名称
                            + "     <td align=\"center\" style='word-wrap:break-word'>"+info.for_workshop+"</td>"//使用车间
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_num + info.unit + "</td>"//数量/单位
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.sizes + "</td>"//规格
                            + "     <td align=\"center\" style='word-wrap:break-word'></td>"//单价
                            + "     <td align=\"center\" style='word-wrap:break-word'>订单号:" + info.erp_no + "</br>"//用途
                            + "     机型:" + conProductNum + "</br>"
                            + "     数量:" + sdOrderAmount + "</br>"
                            + "     发往地:" + cusSentToName + "</br>"
                            + "     事业部:" + conDept
                            + "    </td>"
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.apply_reason + "</td>"//申请原因
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + new Date(info.demand_time).format('yyyy-MM-dd') + "</td>"//需求日期
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.stock_num + "</td>"//确认库存数量
                            + "     <td align=\"center\" style='word-wrap:break-word'>" + info.remark + "</td>"//备注
                            + "    </tr>";
                    }
                    SerialNumber++;
                })
                var strArr = apply_man.split(" ");//把字符串分割成一个数组
                strArr.sort();//排序
                var result = new Array();//创建出一个结果数组
                var tempStr = "";
                for (var i in strArr) {
                    if (strArr[i] != tempStr) {
                        result.push(strArr[i]);
                        tempStr = strArr[i];
                    } else {
                        continue;
                    }
                }
                apply_man = result.join(" ");//间隔空格拼接

                //使用部门
                var deptStrArr = deptUse.split(" ");//把字符串分割成一个数组
                deptStrArr.sort();//排序
                var deptResult = new Array();//创建出一个结果数组
                var tempDeptStr = "";
                for (var j in deptStrArr) {
                    if (deptStrArr[j] != tempDeptStr) {
                        deptResult.push(deptStrArr[j]);
                        tempDeptStr = deptStrArr[j];
                    } else {
                        continue;
                    }
                }
                deptUse = deptResult.join(" ");

                var serialNum = PrefixInteger(row_apply.apply_order_info_id, 6);

                nw = window.open();
                //将表格放在div里，然后把整个div里面的内容给赋给一个变量。
                var tab_html = '<div id="sendTitle" style="width:1150px;margin:0 auto;height:30px;"><img style="width:420px;height:60px;" src="/psystem/static/images/tablelogo.png"/></div>'
                    + "<div style=\"width:1150px;margin:0 auto\">"
                    + "     <p align=\"center\" style=\"font-size:40px;font-weight: bold;\">请购单</p>"
                    + "  <div width=\"1150px\" style=\"font-size:20px;\" style=\"margin-bottom:10px;\">"
                    + "    <span style=\"margin-right:30px;\">申购部门:" + row_apply.apply_dept + "</span>"
                    + "    <span style=\"margin-right:30px;\">投入部门:" + deptUse + "</span>"
                    + "    <span style=\"margin-right:30px;\">申购人:" + apply_man + "</span>"
                    + "   <span style=\"margin-right:20px;\">申请日期:" + new Date(row_apply.apply_time).format('yyyy-MM-dd') + "</span>"
                    + "   <span style=\"margin-right:20px;\">表单序号:" + new Date(row_apply.apply_time).format('yyyyMMdd') + serialNum + "</span>"
                    + "  </div>"
                    + "  <div width=\"1150px\" style=\"font-size:20px;\">"
                    + "   <span>申购类型:"
                    + "   <input type=\"checkbox\" name=\"temp\" value=\"临时\" />临时"
                    + "   <input type=\"checkbox\" name=\"new\" checked=\"true\" value=\"新增\" style=\"margin-left:50px;\"/>新增"
                    + "   <input type=\"checkbox\" name=\"special\" value=\"订单特殊需求\" style=\"margin-left:50px;\" />订单特殊需求"
                    + "   <input type=\"checkbox\" name=\"temp\" value=\"外部维修\" style=\"margin-left:50px;\" />外部维修"
                    + "   </span>"
                    + urgentTrString
                    + " </div>"
                    + "  </div>"
                    + "  <table border=\"1\" cellspacing=\"0\" width=\"1150px\"  style=\"margin:0 auto;table-layout:fixed\">"
                    + "   <tbody>"
                    + "    <tr style=\"font-size:15px;\">"
                    + "     <th align=\"center\" width=\"20px;\">序号</th>"
                    + "     <th align=\"center\" width=\"80px;\">物资编号</th>"
                    + "     <th align=\"center\" width=\"90px;\">物品名称</th>"
                    + "     <th align=\"center\" width=\"40px;\">使用车间</th>"
                    + "     <th align=\"center\" width=\"50px;\">数量/单位</th>"
                    + "     <th align=\"center\" width=\"150px;\">规格</th>"
                    + "     <th align=\"center\" width=\"50px;\">单价</th>"
                    + "     <th align=\"center\" width=\"170px;\">用途</th>"
                    + "     <th align=\"center\" width=\"180px;\">申请原因</th>"
                    + "     <th align=\"center\" >需求日期</th>"
                    + "     <th align=\"center\" width=\"35px;\">确认库存数量</th>"
                    + "     <th align=\"center\" width=\"200px;\">备注</th>"
                    + "    </tr>" +

                    // + "    <tr style=\"font-size:15px;\">"
                    // + "     <th align=\"center\" >序号</th>"
                    // + "     <th align=\"center\" >物资编号</th>"
                    // + "     <th align=\"center\" >物品名称</th>"
                    // + "     <th align=\"center\" >数量/单位</th>"
                    // + "     <th align=\"center\" >规格</th>"
                    // + "     <th align=\"center\" >单价</th>"
                    // + "     <th align=\"center\" >用途</th>"
                    // + "     <th align=\"center\" >申请原因</th>"
                    // + "     <th align=\"center\">需求日期</th>"
                    // + "     <th align=\"center\" >确认库存数量</th>"
                    // + "     <th align=\"center\">备注</th>"
                    // + "    </tr>" +

                    // + "    <tr id=\"item01\">"
                    trString
                    + " <tr height=\"150px\">"
                    + " <td colspan=\"12\" style=\"text-align:left;vertical-align:top;\">具体要求:</td>"
                    + "</tr>"
                    + "<tr height=\"40px\">"
                    + "  <td colspan=\"5\">预算资金:</td>"
                    + "  <td colspan=\"3\">结算资金:</td>"
                    + "  <td colspan=\"4\">费用归属中心（部门）审核:</td>"
                    + "</tr>"
                    + "<tr height=\"40px\">"
                    + "<td colspan=\"5\">部门经理审核:</td>"
                    + "<td colspan=\"3\">副总经理审核:</td>"
                    + "<td colspan=\"4\">总经理审核:</td>"
                    + "</tr>"
                    + "<tr height=\"40px\">"
                    + "  <td colspan=\"5\">报价初核:</td>"
                    + "  <td colspan=\"7\">报价批准:</td>"
                    + "</tr>"
                    + "</tbody>"
                    + "</table>"
                    + "<div style=\"width:1150px;margin:0 auto;\">"
                    + "<p>说明：签名时，请签上日期。</p>"
                    + "</div>"
                    + "<div style=\"width:1150px;margin:0 auto;\">"
                    + " <p align=\"right\" style=\"height:10px;\">表单编号：UM-FM-ED-175B</p>"
                    + " <p align=\"right\" style=\"height:10px;align:right;\">保存期限：3年</p>"
                    + "</div>";
                var tit_html = '<div id="sendTitle" style="width:1150px;"><center><span style="font-size:24px"><b></b></span></center></div>';
                tab_html = tab_html.replace(/disabled/g, "");
                tab_html = tab_html.replace(/<TEXTAREA/g, '<TEXTAREA readonly style="border:0px 0px 0px 0px;"');
                tab_html = tab_html.replace(/<INPUT/g, '<INPUT readonly style="border:0px 0px 0px 0px;" ');
                tab_html = tab_html.replace(/type=checkbox/g, 'type=checkbox onClick="return false;"');
                tab_html = tab_html.replace(/type=button/g, 'type=button style="visibility:hidden"');
                //console.log(tab_html);
                //document.getElementsByTagName('body')[0].style.zoom=0.92;
                nw.document.open("text/html", "GB2312");
                nw.document.write("<style media=print>.Noprint{display:none;}.PageNext{page-break-BEFORE: always;} html{zoom:80%;}</style>");
                nw.document.write(tit_html);//打印第一个表格
                nw.document.write(tab_html);//打印第一个表格
                // nw.document.write('<div  style="margin:0 auto;width:100px;"><input type="button" class="Noprint" onclick = "javascript:window.print()" value="确定打印" /></div>');
                //nw.location.reload();
                //nw.document.close();
            }
        },
        complete: function () {

        }
    });


    /*document.body.innerHTML=document.getElementById('dlg_apply_form').innerHTML;
    window.print();*/
}
